package com.pinyougou.shop.controller;

import com.pinpougou.commom.util.FastDFSClient;
import com.pinyougou.vo.Result;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/upload")
public class UploadController {
    @PostMapping
    public Result upload(MultipartFile file) {
        Result result = Result.fail("上传文件失败");
        try {
            //上传图片文件
            FastDFSClient fastDFSClient = new FastDFSClient("classpath:fastdfs/tracker.conf");
            //文件扩展名
            String file_ext_name = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
            String url = fastDFSClient.uploadFile(file.getBytes(), file_ext_name);
            return Result.ok(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }
}
